|
|
|
|
|
|
|
After the MoveNext method is executed, the pointer may now be at the end of the file (EOF). If so, no current record exists. Therefore, if the end of the file is encountered, a MoveLast method is used to make sure the record pointer is positioned at the last record in the recordset.  |
|
|
|
|
|
|
|
|
In addition to processing records with a program loop, you can use SQL statements to handle a number of functions that apply to multiple records. The following sections discuss two main types of functions: |
|
|
|
|
|
|
|
|
Calculation queries, which provide cumulative information about the requested group of records. |
|
|
|
|
|
|
|
|
Action queries that insert, delete, or modify groups of records in a recordset. |
|
|
|
|
|
|
|
|
For a more in-depth discussion of the SQL language, see Appendix C, "SQL Summary." |
|
|
|
|
|
|
|
|
Calculation queries allow you to determine cumulative information about a group of records such as the total; average, minimum, and maximum values; and the number of records. They work by specifying calculation functions built-in to the SQL language. You can also specify the filter criteria for the records using the standard WHERE clause. For example, you can extract total sales for all salesmen in the Southeast region or the maximum price of a stock on a given day (assuming, of course, that the base data is in your tables). |
|
|
|
|
|
|
|
|
Suppose you have a database table that contains records for fish sales. Each record has a code to identify the amount of the sale and the type of fish. Consider the following lines of VB code: |
|
|
|
|
|
|
|
|
sSQL = "SELECT SUM([Price]) As GrandTotal FROM FishSales WHERE Fishcode = 1001"
Set rsTotal = dbFish.OpenRecordset(sSQL)
Msgbox "Grand Total = " & rsTotal("GrandTotal") |
|
|
|
|
|
|
|
|
The key to understanding the example SQL statement is the SUM function, which adds each value of the Price field together. The returned recordset has a single field called GrandTotal, which contains the result. |
|
|
|
|
|
|
|
|
In addition to SUM, there are several other SQL functions which return calculated values. For example, the following SQL statement could be used to calculate the minimum, maximum, and average price of fish from the table: |
|
|
|
|
|
|
|
|
SELECT MIN(Price) As MinPrice, AVG(Price) As AvgPrice, MAX(Price) As MaxPrice
FROM FishSales |
|
|
|
|
|
|
|
|
The calculation query produces a dynaset with a single record containing the results. Using a calculation query can replace many lines of program code that would be required to produce the same results. In addition, a query is usually faster than the equivalent program code because the calculation is performed by the database engine rather than your own Visual Basic code. |
|
|
|
|
|
|
|
|
Action queries operate directly on a recordset to insert, delete, or modify groups of records based on specific criteria. As with calculation queries, action queries perform the same work that would require several lines of program code. The following lists several types of action queries: |
|
|
|
|
|